package com.huilian.iotbox.data.cmd;

import com.huilian.iotbox.data.constants.IotConstant;
import com.huilian.iotbox.data.po.MachineAppVersion;
import com.huilian.iotbox.data.utils.IotBoxUtil;
import com.huilian.iotbox.data.vo.CommodityStoreUpDownVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * RespondDataFrameCMD
 * 应答终端数据帧
 *
 * @Author mineChen
 * @Date 2021/8/30 下午 03:41
 */
@Slf4j
public class RespondDataFrameCMD {

    /**
     * 1.2、心跳包（服务器to支付盒子）
     * 响应心跳指令
     *
     * @param isPermit 是否允许登录
     * @return
     */
    public static String heart(Boolean isPermit) {
        StringBuilder stringBuilder = new StringBuilder(IotConstant.CONTENT_PREFIX);
        StringBuilder dataPrefix = new StringBuilder("160001");
        final String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMddHHmmss"));
        final StringBuilder dataAre = dataPrefix
                .append(IotBoxUtil.date2Hex(timestamp))
                .append(isPermit ? "01" : "00")
                .append(IotBoxUtil.complement(12));

        stringBuilder
                .append(dataAre)
                .append(IotBoxUtil.checkcode_0007(dataAre.toString()))
                .append(IotConstant.CONTENT_SUFFIX);
        return stringBuilder.toString().toUpperCase();
    }

    /**
     * 31.1、远程烧号指令（服务器to支付盒子）
     *
     * @param deviceNo    设备号
     * @param pwdDeviceNo 设备密钥
     * @param productKey  产品密钥
     * @return
     */
    public static String remoteWriteDeviceNoCmd(String deviceNo, String productKey, String pwdDeviceNo) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("00f1");
        String deviceNoStr = deviceNo + "-" + productKey + "-" + pwdDeviceNo + "-";
        stringBuilder.append(IotBoxUtil.convertStringToHex(deviceNoStr)); //三元素转ASCII
        stringBuilder.insert(0, IotBoxUtil.singleInt2Hex(stringBuilder.toString().length()));
        stringBuilder.append(IotBoxUtil.checkcode_0007(stringBuilder.toString()));
        stringBuilder.insert(0, IotConstant.CONTENT_PREFIX);
        stringBuilder.append(IotConstant.CONTENT_SUFFIX);
        return stringBuilder.toString().toUpperCase();
    }



    /**
     * 拼装套餐，金额（分），游戏币（枚）
     *
     * @param commodityStoreUpDownVoList
     * @return
     */
    public static String syncPackage(List<CommodityStoreUpDownVo> commodityStoreUpDownVoList) {
        // 套餐数量
        final int packageCount = commodityStoreUpDownVoList.size();
        StringBuilder packageData = new StringBuilder(IotBoxUtil.singleInt2Hex(packageCount));
        for (CommodityStoreUpDownVo commodityStoreUpDownVo : commodityStoreUpDownVoList) {
            final BigDecimal price = commodityStoreUpDownVo.getRetailPrice().multiply(BigDecimal.valueOf(100));
            log.info("price:" + price);
            packageData.append(IotBoxUtil.reverseHex(IotBoxUtil.singleInt2Hex_2(price.intValue(), 3)));

            if (StringUtils.isEmpty(commodityStoreUpDownVo.getRechargeCoin())) {
                commodityStoreUpDownVo.setRechargeCoin(0);
            }
            if (StringUtils.isEmpty(commodityStoreUpDownVo.getGiveCoin())) {
                commodityStoreUpDownVo.setGiveCoin(0);
            }
            final int sumCoins = commodityStoreUpDownVo.getRechargeCoin() + commodityStoreUpDownVo.getGiveCoin();
            log.info("sumCoins:" + sumCoins);
            packageData.append(IotBoxUtil.reverseHex(IotBoxUtil.singleInt2Hex_2(sumCoins, 3)));
        }

        final int len = packageData.toString().length() + 10;
        StringBuilder dataPrefix = new StringBuilder(IotBoxUtil.singleInt2Hex(len));
        dataPrefix.append("01");
        dataPrefix.append("000106");
        dataPrefix.append(packageData);

        StringBuilder stringBuilder = new StringBuilder(IotConstant.CONTENT_PREFIX)
                .append(dataPrefix)
                .append(IotBoxUtil.checkcode_0007(dataPrefix.toString()))
                .append(IotConstant.CONTENT_SUFFIX);
        return stringBuilder.toString().toUpperCase();
    }

    public static void main(String[] args) {
        for (int j = 0; j < 999; j++) {
            List<CommodityStoreUpDownVo> commodityStoreUpDownVoList = new ArrayList<>();
            Random random = new Random();
            for (int i = 1; i < random.nextInt(6); i++) {
                CommodityStoreUpDownVo c = new CommodityStoreUpDownVo();
                c.setRetailPrice(new BigDecimal(0.01));
                c.setRechargeCoin(random.nextInt(9999));
                c.setGiveCoin(random.nextInt(9999));
                commodityStoreUpDownVoList.add(c);
            }
            // AA4800000106050100000A00000100000A00000100000A00000100000A00000100000A000041DD
            System.out.println(RespondDataFrameCMD.syncPackage(commodityStoreUpDownVoList));
        }
    }
}
